import re
import os
from io import BytesIO
from urllib.parse import urlparse
from pycurl import Curl
import certifi

if __name__ == '__main__':
    url = 'https://www.xiachufang.com/'
    buffer = BytesIO()
    c = Curl()
    c.setopt(c.CAINFO, certifi.where())
    c.setopt(c.URL, url)
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()

    text = buffer.getvalue().decode('utf-8')
    img_list = re.findall(r'src=\"(https://i2\.chuimg\.com/\w+\.jpg)', text)

    # 初始化本地存储目标
    img_dir = os.path.join(os.curdir, 'images-index')
    if not os.path.isdir(img_dir):
        os.mkdir(img_dir)
    # 批量获取文件名称
    for img in img_list[::-1]:
        o = urlparse(img)
        filepath = os.path.join(img_dir, o.path[1:])
        # 获取原始图片地址
        # url = '%s://%s%s' % (o.scheme, o.netloc, o.path)
        # print(url)
        # 开始下载图片
        # print(img)
        with open(filepath, 'wb') as f:
            c = Curl()
            c.setopt(c.CAINFO, certifi.where())
            c.setopt(c.URL, img)
            c.setopt(c.WRITEDATA, f)
            c.perform()
            c.close()
